﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2Logic;

namespace Ras2Win
{
    /// <summary>
    /// Klasinn EditHitlist
    /// </summary>
    public partial class EditHitList : Form
    {
        #region Member breytur

        /// <summary>
        /// Member breyta sem inniheldur eitt HitList object.
        /// </summary>
        private HitList m_hitlist;

        #endregion

        #region Smiðir

        /// <summary>
        /// Færibreytulaus smiður.
        /// </summary>
        public EditHitList()
        {
            InitializeComponent();
        }

        #endregion

        #region Get / Set föll

        /// <summary>
        /// GetSet fall fyrir breytuna HitList sem er af taginu HitList.
        /// </summary>
        public HitList HitList
        {
            get
            {
                m_hitlist.ID = Convert.ToInt32(m_txtHitListID.Text);
                m_hitlist.Name = m_txtHitListName.Text;
                m_hitlist.DateBegins = m_dtpHitListBeginDate.Value;
                m_hitlist.DateEnds = m_dtpHitListEndDate.Value;
                return m_hitlist;
            }
            set
            {
                m_hitlist = value;
                m_txtHitListID.Text = Convert.ToString(m_hitlist.ID);
                m_txtHitListName.Text = m_hitlist.Name;
                if ((m_hitlist.DateBegins == null) || (m_hitlist.DateEnds == null))
                {
                    m_dtpHitListBeginDate.Value = DateTime.Now;
                    //Hef 7 daga á milli Begin og End default. Notandi getur svo breytt sjálfur.
                    TimeSpan timeSpan = new TimeSpan(7, 0, 0, 0);
                    m_dtpHitListEndDate.Value = DateTime.Now.Add(timeSpan);
                }
                else
                {
                    m_dtpHitListBeginDate.Value = (DateTime)m_hitlist.DateBegins;
                    m_dtpHitListEndDate.Value = (DateTime)m_hitlist.DateEnds;
                }

            }
        }

        #endregion

        #region Private föll

        /// <summary>
        /// EditHitList_Load keyrir þegar edit glugginn er smíðaður.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditHitList_Load(object sender, EventArgs e)
        {
            //Setjum lögin á listanum inn í listview.
            if (!this.DesignMode)
            {
                HitListController ctrl = new HitListController();
                IEnumerable<vGetAllHitListEntry> hitList = ctrl.GetAllHitListEntries(m_hitlist.ID);

                foreach (var item in hitList)
                {
                    m_lstHitListSongs.Items.Add(CreateListViewItem(item));
                }
            }
        }

        /// <summary>
        /// Hjálpar fall sem smíðar listview item sem er svo sett inn í listviewið.
        /// </summary>
        /// <param name="songsInHitList">Object af taginu vGetAllHitListEntry</param>
        /// <returns>Skilar ListView itemi</returns>
        private ListViewItem CreateListViewItem(vGetAllHitListEntry songsInHitList)
        {
            ListViewItem item = new ListViewItem(Convert.ToString(songsInHitList.Position));
            item.SubItems.Add(songsInHitList.SongName);

            item.ImageIndex = 0;
            item.Tag = songsInHitList;
            return item;
        }

        /// <summary>
        /// Fall fyrir breytingu á hitlista.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_btnChangeHitList_Click(object sender, EventArgs e)
        {
            //Opna glugga með lista yfir lög.
            //Velja lagið þar í listanum og ýta á OK takka.
            //Byrja á að opna annað dialog með song listanum.
            Song song = new Song();
            using (SelectSong dlg = new SelectSong())
            {
                //"Innrétta" dlg.Song með song objecti.
                dlg.Song = song;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    HitListEntryController ctrlHLE = new HitListEntryController();
                    HitListEntry hitListEntry = new HitListEntry();

                    //Erum bara að adda nýjum færslum. Þarf að útfæra öðruvísi upp á að edita.
                    hitListEntry.ID = 0;
                    hitListEntry.SongID = dlg.SongID;
                    hitListEntry.HitListID = Convert.ToInt32(m_txtHitListID.Text);
                    hitListEntry.Position = Convert.ToInt32(dlg.ListSeat);

                    //Adda svo entryinu inn í database
                    ctrlHLE.AddHitListEntry(hitListEntry);

                    //Hreinsum listviewið og setjum allt inn aftur
                    HitListController ctrlHL = new HitListController();
                    IEnumerable<vGetAllHitListEntry> hitList = ctrlHL.GetAllHitListEntries(m_hitlist.ID);
                    m_lstHitListSongs.Items.Clear();
                    foreach (var item in hitList)
                    {
                        m_lstHitListSongs.Items.Add(CreateListViewItem(item));
                    }
                }
            }
        }

        #endregion

        #region Public föll

        #endregion
    }
}
